﻿// Includes
#include "template.h"

#include"Blanking.h"
#include"TriangleFill.h"

#include<string>
#include"Space.h"
#include "CG.h"


// Namespace
using namespace AGK;
using namespace std;

app App;
CG cg;

void app::Begin(void)
{
	agk::SetVirtualResolution(DEVICE_WIDTH, DEVICE_HEIGHT);
	agk::SetWindowTitle("Computer Graphics");
	agk::SetWindowAllowResize(0);
	agk::SetClearColor( 255,255,255 );
	agk::SetSyncRate(60,1);
	agk::SetScissor(0,0,0,0);
	agk::UseNewDefaultFonts(1);
	agk::SetPrintColor(0, 255, 0);
	//C:\Users\lzw\AppData\Local\AGKApps\My Company\Template64
	agk::SetRawWritePath("F:/ProjectFiles/Visual Studio 2017/c++/学习计算机图形学/Final");
	agk::SetPhysicsThreading(5);


	mouseX = -1;
	mouseY = -1;
	cg.Init();	
}


int app::Loop (void)
{
	if (agk::GetRawKeyReleased(AGK_KEY_F1) == true)//存储照片
	{
		std::string path = "/";
		char* time = agk::GetCurrentTime();
		time[2] = time[5] = '-';
		path = path + agk::GetCurrentDate() + " " + time + ".png";
		agk::SaveImage(1, path.c_str());
	}
	if (agk::GetRawKeyReleased(AGK_KEY_F2) == true)//刷新
	{
		cg.Refresh();
	}
	if (agk::GetRawKeyReleased(AGK_KEY_F3) == true)//开关持续刷新
	{
		cg.SwitchAlwaysRefresh();
	}
	if (agk::GetRawKeyReleased(AGK_KEY_F4) == true)//切换绘制模式
	{
		cg.ChangeDrawMode();
	}
	if (agk::GetRawKeyReleased(AGK_KEY_F5) == true)//切换渲染方式
	{
		cg.ChangePhongMethod();
	}	

	if (agk::GetRawMouseRightState())//移动视角
	{
		if (mouseX != -1 && mouseY != -1)
		{
			cg.View(0.1*(mouseX - agk::GetRawMouseX())*PI / 180, 0.1*(mouseY - agk::GetRawMouseY())*PI / 180);
		}
	}
	mouseX = agk::GetRawMouseX();
	mouseY = agk::GetRawMouseY();

	float moveSpeed = 10;
	//移动
	if (agk::GetRawKeyState(AGK_KEY_W))
	{
		cg.Move(0, 0, moveSpeed);
	}
	if (agk::GetRawKeyState(AGK_KEY_S))
	{
		cg.Move(0, 0, -moveSpeed);
	}
	if (agk::GetRawKeyState(AGK_KEY_A))
	{
		cg.Move(-moveSpeed, 0, 0);
	}
	if (agk::GetRawKeyState(AGK_KEY_D))
	{
		cg.Move(moveSpeed, 0, 0);
	}
	if (agk::GetRawKeyState(AGK_KEY_SPACE))
	{
		cg.Move(0, moveSpeed, 0);
	}
	if (agk::GetRawKeyState(AGK_KEY_CONTROL))
	{
		cg.Move(0, -moveSpeed, 0);
	}

	if (cg.isLoop)
	{
		cg.Func();
		//保存图片
		if (agk::GetImageExists(1))
			agk::DeleteImage(1);
		agk::GetImage(1, 0, 0, DEVICE_WIDTH, DEVICE_HEIGHT);
		if (agk::GetSpriteExists(1))
			agk::SetSpriteImage(1, 1);
		else
			agk::CreateSprite(1, 1);
	}
	agk::Print(agk::ScreenFPS());
	agk::Sync();

	return 0; // return 1 to close app
}


void app::End (void)
{
	cg.Destory();
}